unit Log4D;

interface

uses
  Classes,SyncObjs;

type
  ELogLeve = (Debug,Info,Warn,Error);

  TLog4D = class
  private
    OutCon: Boolean;
    FLog: TStrings;
  public
    class function getInstance():TLog4D;
    procedure SetLogCon(ALogC: TStrings);
    procedure Log(AMsg: String ;ALeve:ELogLeve);
    procedure SaveToFile(AFileName: String);
    constructor Create();
    destructor Destroy(); override;
  end;

var
  LogCS: TCriticalSection;

implementation

var
  Log4D_Instance: TLog4D = nil;

{ TLog4D }

constructor TLog4D.Create;
begin
  LogCS := TCriticalSection.Create;
  FLog := TStringList.Create;
  OutCon := false;
end;

destructor TLog4D.Destroy;
begin
  if not OutCon then
    FLog.Free;
  LogCS.Free;
  inherited;
end;

class function TLog4D.getInstance: TLog4D;
begin
  if not Assigned(Log4D_Instance) then
    Log4D_Instance := TLog4D.Create;
  Result := Log4D_Instance;
end;

procedure TLog4D.Log(AMsg: String; ALeve: ELogLeve);
begin
  LogCS.Enter;
  try
    case ALeve of
     Debug: AMsg := '[DEBUG]' + AMsg;
     Info:  AMsg := '[INFO]' + AMsg;
     Warn:  AMsg := '[WARN]' + AMsg;
     Error: AMsg := '[ERROR]' + AMsg;
    end;
    FLog.Add(AMsg);
  finally
    LogCS.Leave;
  end;

end;

procedure TLog4D.SaveToFile(AFileName: String);
begin
  FLog.SaveToFile(AFileName);
end;

procedure TLog4D.SetLogCon(ALogC: TStrings);
begin
  FLog.Free;
  FLog := ALogC;
  OutCon := True;
end;

end.
